/*
 * Created on Jul 30, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.herohead;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.CopyUtils;
import org.apache.log4j.Logger;

/**
 * FS.class - This class contains Fourth Shift Related classes.
 * 
 * @author  Craig David mailto:Craig.david@mt.com
 * 
 */

@SuppressWarnings("deprecation")
public class FS {

	static Properties conf=new Properties();

	static Logger logger = Logger.getLogger(Support.class);

	/**
	 * FS2MOFT move files from one directory to the extraction directory in the CONF file.
	 * 
	 * @param src
	 * @param des
	 */

	//	########################################################
	public static void FS2MOFT(String src, String des) {
		//	########################################################


		File srcDir = new File(src);
		String srcFilename = srcDir.getName();
		//String srcDirectory = srcDir.getParent();

		//Support sup = new Support();
		Store extract = new Store();

		//String fleList[];

		try {
			if (srcDir.exists()) {
				if (!srcDir.canWrite() || !srcDir.canRead() || srcDir.isHidden())  {
					logger.warn("File " + des + " is locked. Skipping file");
				} else
					logger.debug("Here is the des " + des);
				extract.setDir(des);
				byte[] b;
				b = Support.getBytesFromFile(srcDir);
				extract.storeFile(b,srcFilename);
				srcDir.delete();
			} else {
				logger.info("No files found to process");
			}


		} catch (IOException e)
		{
			logger.warn("No files found :" + e );
		}
	}

	/**
	 * MOFT2FS - move files from the MOFT collection directory and move them to the FS file in the CONF file.
	 * 
	 * @param collectionDir
	 * @param destination
	 */
	public static void MOFT2FS(String collectionDir, String destination) {
		//	########################################################

		/**
		 * @docRoot - this is the doc root.
		 */

		Support sup = new Support();
		//Store store = new Store();

		String[] fleList;
		try {
			fleList  = sup.check4Files(collectionDir);

			File dest = new File(destination);
			BufferedWriter out = new BufferedWriter(new FileWriter(dest, true));

			if (fleList.length > 0) {
				for (int i = 0; i < fleList.length; i++) {
					File fle = new File(collectionDir + "/" + fleList[i]);
					if (!fle.canWrite() || !fle.canRead() || fle.isHidden())  {
						logger.warn("File " + collectionDir + "/" + fleList[i] + " is locked. Skipping file");
					} else
					{
						logger.info("Found " + fleList[i] + " in " + collectionDir);
						File tmp = new File(collectionDir + fleList[i]);
						//boolean success = dest.createNewFile();
						BufferedReader in = new BufferedReader(new FileReader(collectionDir + fleList[i]));
						logger.debug("Appending " + collectionDir + " to " + collectionDir);
						CopyUtils.copy(in,out);
						in.close();
						logger.info("File delete " + collectionDir + fleList[i]);
						tmp.delete();
					}
				}
				out.close();
			}
		} catch (Exception e)
		{
			logger.warn("No files found :" + e );
		} catch (Error e)
		{
			logger.info("No files found :" + e );
		}
	}
	//	########################################################
	public void remove72rec(String fsFile) {
		//	########################################################

		try {

			BufferedReader in = new BufferedReader(new FileReader(fsFile));

			BufferedWriter out = new BufferedWriter(new FileWriter(fsFile + ".tmp"));

			String str;

			while (in.ready()) {
				str = in.readLine();
				if (str.length() >2) {
					if (str.substring(2,4).compareTo("er") != 0) {
						out.write(str);
					}
				}
			}

			in.close();
			out.flush();
			out.close();

			//File fout = new File(fsFile + ".tmp");
			File fin = new File(fsFile);

			System.out.println(fin.getPath());

			System.out.println(fin.delete());

			if (fin.delete()) {
				System.out.println("delete good");
			} else {
				System.out.println("Delete bad");
			}

			//fout.renameTo(new File(fsFile));


		} catch (Exception e)
		{
			System.out.println(e);
			//logger.warn("Error: " + e );
		}

	}
}